package com.yuanfeng.job.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuanfeng.commoms.util.kuaidiniao.KdniaoTrackQueryAPI;
import com.yuanfeng.job.dto.ActivityNoneRecSlideDTO;
import com.yuanfeng.job.mapper.AppJiPushMapper;
import com.yuanfeng.job.vo.AppJiPushOrderVO;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AppJiPushOrderJob
 * @Description App推送  订单签收推送
 * @Author liulh
 * @Date 2023/5/17 14:01
 * @Version 1.0
 **/
public class AppJiPushOrderJob implements BaseJob{

    @Autowired
    private AppJiPushMapper appJiPushMapper;

    private static final Logger logger = LoggerFactory.getLogger(AppJiPushOrderJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        System.out.println("AppJiPushOrderJob 订单签收推送");
        KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
        List<AppJiPushOrderVO> logisticsList = appJiPushMapper.queryOrderLogistics();
        List<String> userIdList = new ArrayList<>();
        ActivityNoneRecSlideDTO activity = new ActivityNoneRecSlideDTO();
        try {
            for (AppJiPushOrderVO list : logisticsList) {
                String result = KdniaoTrackQueryAPI.getOrderTracesByJson(list.getExpressPinyin(), list.getOrderShippingCode(),"");
                JSONObject jsonObject = JSON.parseObject(result);
                if(jsonObject.get("State") != null && !"".equals(jsonObject.get("State"))){
                    if (jsonObject.get("State").equals("3")) {
                        userIdList.add(list.getBuyerUserId());
                    }
                }
            }
            if (userIdList.size() > 0) {
                System.out.println(userIdList.size());
                activity.setActionType(40);
                String extra = JSONObject.toJSONString(activity);
//                JiPushUtil.IOSSendToAliasList(userIdList, "", " ", "您的订单已签收，请尽快核实商品", extra);
//                JiPushUtil.AndoridSendToAliasList(userIdList, "", " ", "您的订单已签收，请尽快核实商品", extra);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("AppJiPushOrderJob 完成订单签收推送");

    }

}
